<template>
    <div class="sign">
        <h2>注册</h2>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="邮箱:" prop="buyerEmail">
                <el-input v-model="ruleForm.buyerEmail"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" auto-complete="new-password"></el-input>
            </el-form-item>

            <el-form-item label="确认密码:" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="手机号:" prop="number">
                <div class="phone">
                    <el-select v-model="ruleForm.region" placeholder="+86" class="inp">
                        <el-option label="+86" value=""></el-option>
                        <el-option label="+01" value=""></el-option>
                        <el-option label="+02" value=""></el-option>
                    </el-select>
                    <el-input v-model.number="ruleForm.number" placeholder="请输入手机号"></el-input>
                </div>
            </el-form-item>

            <el-form-item label="验证码:" prop="code">
                <div class="code">
                    <el-input placeholder="输入验证码" class="inpcode" v-model.code="ruleForm.code">
                    </el-input>
                    <button class="getCAPTCHA send" size="small" type="primary" @click="getAuthCode">发送验证码</button>
                </div>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click.native.prevent="submitForm('ruleForm')">注册</el-button>

                <el-button @click="existcountlogin('ruleForm')">使用已有账户登录</el-button>
            </el-form-item>
        </el-form>

    </div>
</template>

<script>
    export default {
        name: "Sign",
        data() {
            // 检测手机号
            var checknumber = (rule, value, callback) => {
                const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
                if (!value) {
                    return callback(new Error('号码不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {

                        if (phoneReg.test(value)) {
                            callback()
                        } else {
                            callback(new Error('电话号码格式不正确'))
                        }

                    }
                }, 500);
            };

            //检测密码
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            //邮箱校验
            var checkEmail = (rule, value, callback) => {
                const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
                if (!value) {
                    return callback(new Error('邮箱不能为空'))
                }
                setTimeout(() => {
                    if (mailReg.test(value)) {
                        callback()
                    } else {
                        callback(new Error('请输入正确的邮箱格式'))
                    }
                }, 500)
            }
            //确认密码
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    buyerEmail: '',
                    pass: '',
                    checkPass: '',
                    number: '',
                    code: ""
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    number: [
                        { validator: checknumber, trigger: 'blur' }
                    ],
                    buyerEmail: [
                        { validator: checkEmail, trigger: 'blur' }
                    ]

                }
            };
        },
        methods: {
            //注册提交
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                        console.log(formName, valid)
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
                this.$router.push("/countlogin")
            },
            existcountlogin(formName) {
                this.$router.push("/countlogin")
            },
            //获取验证码--调用获取短信验证码接口
            getAuthCode: function () {
                const verification = this.ReginForm.tel;
                const url = " "
                console.log("url", url);
                this.$http.get(url).then(function (response) {
                    console.log("请求成功", response)
                }, function (error) {
                    console.log("请求失败", error);
                })
                //设置倒计时时间
                // this.sendAuthCode = false;
                // //设置倒计时秒
                // this.auth_time = 10;
                // var auth_timetimer = setInterval(() => {
                //     this.auth_time--;
                //     if (this.auth_time <= 0) {
                //         this.sendAuthCode = true;
                //         clearInterval(auth_timetimer);
                //     }
                // }, 1000);

                // let obj = {
                //   mobile: this.tel,
                //   sms_type: "login"
                // };
                // //调用验证码短信接口
                // code(obj).then(res => {
                //   console.log(res)
                // })

            },
        }
    }
</script>

<style scoped>
    .header {
        width: 100%;
        height: 100px;
        background: #ddd;
    }

    .left {
        float: left;
    }

    .right {

        float: right;
        width: 100px;
        height: 100px;
    }

    .demo-ruleForm {
        background: #eee;
        width: 550px;
        margin: auto;
    }

    .el-form-item {
        width: 500px;
    }

    .el-form-item__label {
        align-items: center;
    }

    .phone {
        width: 400px;
        height: 40px;
        display: flex;
    }

    .inp {
        width: 90px;
    }

    .inpcode {
        width: 295px;
        height: 40px;
    }

    .code {
        width: 400px;
        height: 40px;
        display: flex;
    }

    .send {
        width: 100px;
        height: 38px;
        line-height: 38px;
        text-align: center;
        background: #fff;
        border: none;
        border-radius: 2px;
    }
</style>